//
//  PageController.m
//  NVAReader
//
//  Created by DuyLK on 6/14/14.
//  Copyright (c) 2014 com.nva.nvareader. All rights reserved.
//

#import "PageController.h"
#import "PageCell.h"
#define LISTVIEW_CELL_IDENTIFIER		@"PageCell"
@implementation PageController
-(id) initWithDocument:(fz_document *)doc
{
    self = [super init];
    if (self) {
        [listView setCellSpacing: 2.0f];
        //[listView setAllowsEmptySelection: YES];
        [listView setAllowsMultipleSelection: YES];
        [listView registerForDraggedTypes: [NSArray arrayWithObjects: NSStringPboardType, nil]];
        
        _listItems = [[NSMutableArray alloc] init];
        document = doc;
        _totalItem = fz_count_pages(doc);
        // Create a bunch of rows as a test:
        for( NSInteger i = 0; i < _totalItem ; i++ )
        {
            NSString *title = [[NSString alloc] initWithFormat: @"%ld", i +1]; // We're in a tight loop
            [_listItems addObject: title];
            [title release];
        }
    }
    return  self;
}

-(id) initWithDocument:(fz_document *)doc width:(int)widthPage height:(int)heightPage{
    self = [super init];
    if (self) {
        _withPage = widthPage;
        _heightPage = heightPage;
        [listView setCellSpacing: 2.0f];
        //[listView setAllowsEmptySelection: YES];
        [listView setAllowsMultipleSelection: YES];
        [listView registerForDraggedTypes: [NSArray arrayWithObjects: NSStringPboardType, nil]];
        
        _listItems = [[NSMutableArray alloc] init];
        _pageCellTemp = [[NSMutableArray alloc] init];
        document = doc;
        _totalItem = fz_count_pages(doc);
        // Create a bunch of rows as a test:
        for( NSInteger i = 0; i < _totalItem ; i++ )
        {
            NSString *title = [[NSString alloc] initWithFormat: @"%ld", i +1]; // We're in a tight loop
            [_listItems addObject: title];
            [title release];
        }
    }
    return  self;
}

- (void)dealloc
{
	[_listItems release];
    [_pageCellTemp release];
	[super dealloc];
}

- (NSUInteger)numberOfRowsInListView: (PXListView*)aListView
{
	return [_listItems count];
}

- (PXListViewCell*)listView:(PXListView*)aListView cellForRow:(NSUInteger)row
{
	PageCell *cell = (PageCell*)[aListView dequeueCellWithReusableIdentifier:LISTVIEW_CELL_IDENTIFIER];
	
	if(!cell) {
		cell = [[[PageCell alloc] initWithReusableIdentifier:LISTVIEW_CELL_IDENTIFIER] autorelease];
        [_pageCellTemp addObject:cell];
	}
    cell.delegate = self;
	// Set up the new cell:
	[cell setTitle: [_listItems objectAtIndex: row]];
    _currentPage = (int)row;
    [cell setSearchData:_searchData];
    [cell setWidthPage:_withPage];
    [cell setHeightPage:_heightPage];
    [cell setDoc:document];
    [cell setIsZoom:_isZoom];
    //[cell setIsDrawTool:_isDraw];
    //[cell addTextSelectView:_isSelect];
    return cell;
}

- (CGFloat)listView:(PXListView*)aListView heightOfRow:(NSUInteger)row
{
	return _heightPage;
}
-(void)scrollPage:(int)numberPage
{
    [_delegate scrollPage:numberPage];
}
-(void) removeDataSearch
{
    [_searchData release];
    _searchData = nil;
}
-(void) setSearchDataForCell:(NSMutableDictionary *)searchData
{
    if ([_pageCellTemp count] > 0) {
        for (int i = 0; i< [_pageCellTemp count]; i++) {
            PageCell *page = [_pageCellTemp objectAtIndex:i];
            [page setSearchData:searchData];
            [page setNeedsDisplay:YES];
        }
    }
}
-(void) addTextSelectForCell:(BOOL)add{
    if ([_pageCellTemp count] > 0) {
        for (int i = 0; i< [_pageCellTemp count]; i++) {
            PageCell *page = [_pageCellTemp objectAtIndex:i];
            [page addTextSelectView:add];
        }
    }
}

-(void)addDrawHand:(BOOL)add
{
    if ([_pageCellTemp count] > 0) {
        for (int i = 0; i< [_pageCellTemp count]; i++) {
            PageCell *page = [_pageCellTemp objectAtIndex:i];
            [page setIsDrawTool:add];
        }
    }
}

-(void) changeWidthHeight:(int)widthPage height:(int)heightPage
{
    if ([_pageCellTemp count] > 0) {
        for (int i = 0; i< [_pageCellTemp count]; i++) {
            PageCell *page = [_pageCellTemp objectAtIndex:i];
            [page changeWidthHeight:widthPage height:heightPage];
        }
    }
    _withPage = widthPage;
    _heightPage = heightPage;
}
@end
